﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;

namespace coffeeshop.Forms
{
    public partial class frmInputWarehouse : Form
    {
        private ProductInventory productInventory { get; set; }
        public frmInputWarehouse()
        {
            InitializeComponent();
            loadProduct();

            lstProduct.Columns.Add("Tên hàng", 100);
            lstProduct.Columns.Add("Đơn giá", 100);
            lstProduct.Columns.Add("Tồn kho", 90);
            lstProduct.Columns.Add("Đơn vị", 80);
            lstProduct.Columns.Add("ProductId", 0);
        }

        public void loadProduct()
        {
            DataTable dt = ProductInventory.getListProduct();

            lstProduct.Items.Clear();

            lstProduct.View = View.Details;
            lstProduct.FullRowSelect = true;
            lstProduct.GridLines = true;

            foreach (DataRow row in dt.Rows)
            {

                ListViewItem item = new ListViewItem();
                item.Text = (row["Name"].ToString());
                item.SubItems.Add(row["Price"].ToString());
                item.SubItems.Add(row["InStockQuantity"].ToString());
                item.SubItems.Add(row["UnitName"].ToString());
                item.SubItems.Add(row["ProductId"].ToString());
                lstProduct.Items.Add(item);
            }
        }

        private void btnInputWarehouse_Click(object sender, EventArgs e)
        {
            if (lstProduct.SelectedItems.Count > 0)
            {
                int index = int.Parse(lstProduct.SelectedIndices[0].ToString());
                ListViewItem theItem = lstProduct.Items[index];
                string _Name = theItem.Text;
                int _ProductId = int.Parse(theItem.SubItems[4].Text);
                int _Price = int.Parse(theItem.SubItems[1].Text);
                double _InstockQuantity = double.Parse(theItem.SubItems[2].Text);
                string _UnitName = theItem.SubItems[3].Text;
                productInventory = new ProductInventory();
                productInventory.Name = (_Name);
                productInventory.ProductId = _ProductId;
                productInventory.Price = _Price;
                productInventory.UnitName = _UnitName;
                productInventory.InstockQuantity = _InstockQuantity;
                
                frmInputWareHouseDetail inputWareHouseDetail = new frmInputWareHouseDetail(productInventory);
                inputWareHouseDetail.StartPosition = FormStartPosition.CenterParent;
                inputWareHouseDetail.MyParentForm = this;
                inputWareHouseDetail.ShowDialog(this);
            }
            else
            {
                MessageBox.Show("Vui lòng chọn mặt hàng nhập kho.", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void lstProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            setButtonEnable();
        }

        private void setButtonEnable()
        {
            btnInputWarehouse.Enabled = lstProduct.SelectedItems.Count > 0;
        }

        private void btnWarehouseHistory_Click(object sender, EventArgs e)
        {
            frmWareHouseHistorical WareHouseHistorical = new frmWareHouseHistorical();
            WareHouseHistorical.StartPosition = FormStartPosition.CenterParent;
            WareHouseHistorical.ShowDialog(this);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            lstProduct.Items.Clear();
            loadProduct();
        }


    }
}
